<?php

namespace app\company\controller;

use app\admin\model\express\Companys;
use app\admin\model\product\Listo;
use app\admin\model\product\Lists;
use app\admin\model\Transport;
use app\common\controller\CompanyApi;
use app\common\controller\Kd100;
use think\Request;

class Productlisto extends CompanyApi
{
    protected static $model = null;
    protected $noNeedLogin = ['logisticsInfo'];
    protected $noNeedRight = '*';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new Listo();
    }

    /**
     * 获取商品清单表
     */
    public function index()
    {
        $user = $this->auth->getUser();

        $where = [
            'order.status' => ['>=', 1],
        ];

        $department_user_id = $this->request->post('department_user_id');
        $department_id = $this->request->post('department_id');

        $pageSize = $this->request->post('page_size', 15);
        $start_date = $this->request->post('start_date');
        $end_date = $this->request->post('end_date');
        $status = $this->request->post('status');
        if (!$status) {
            $status = [-1];
        } else {
            $status = explode(',', $status);
        }

        $user_ids = $this->getIds($user, $department_user_id, $department_id);
        if (isset($user_ids) && count($user_ids) > 0) {
            $where['order.department_user_id'] = ['in', $user_ids];
        }

        if ($start_date && $end_date) {
            $start_time = strtotime($start_date);
            $end_time = strtotime($end_date);
            $where['listo.pay_time'] = [['>=', $start_time], ['<', $end_time]];
        }

        if (!in_array(-1, $status)) {
            $where['listo.status'] = ['in', $status];
        }

        $lists = self::$model
            ->with(['order'])
            ->field('listo.id,tag,product_id,product_name,num,price,parameters,listo.pay_time')
            ->where($where)
            ->order('listo.id desc')
            ->paginate($pageSize);

        foreach ($lists as &$val) {
            $pInfo = Lists::where('id', $val['product_id'])->find();
//            $val['goodname'] = $pInfo['goodname'];
            $val['cover_ico'] = make_full_url($pInfo['image']);
            $val['intro'] = $pInfo['intro'];

            $val['tool_name'] = Companys::where('id', $val['toolid'])->value('name');

            $uinfo = \app\admin\model\department\User::field('department_id,nickname')->where('id', $val->order->department_user_id)->find();
            $department = \app\admin\model\department\Lists::where('id', $uinfo->department_id)->value('name');

            $val->order->department = $department;
            $val->order->nickname = $uinfo->nickname;
        }

        $this->success('商品清单！', $lists);
    }

    /**
     * 订单物流信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function logisticsInfo()
    {
        $id = input('id');
        $orderInfo = self::$model->where('id', $id)->find();
        if (!$orderInfo) {
            $this->error('该订单不存在！');
        }

        if ($orderInfo->status < 10) {
            $this->error('该订单未发货！');
        }
        if (!$orderInfo->toolid || !$orderInfo->tool) {
            $this->error('快递信息缺失！');
        }

        $code = Companys::where('id', $orderInfo->toolid)->value('code');
        $addressId = \app\admin\model\product\Order::where('id', $orderInfo->order_id)->value('addressid');
        $mobile = Transport::where('id', $addressId)->value('mobile');
        $data = (new Kd100())->logisticsInfo($code, $orderInfo->tool, $mobile);
        if ($data['code'] == 0) {
            $this->error($data['msg']);
        }

        $this->success('物流轨迹为', $data['data']);
    }
}